﻿using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Services;
using System.Security.Cryptography;
using System.Threading;
using System.Collections.Generic;
using XmlToPdf;
using System;

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://localhost:2417/Service1.asmx")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        private const string CACHE_KEY_SALARIES = "salaries";
        private const string SALARY_XML_FILE_PATH = @"\App_Data\salary_{0}.xml.aes";


        [WebMethod]
        public string HelloWorld()
        {
            RijndaelManaged mobjCryptoService = new RijndaelManaged(); 
            return "Hello World";
        }

        [WebMethod]
        public byte[] GetSalaries(string salaryDate)
        {
            HttpContext ctx = HttpContext.Current;
            ctx.Cache.Remove(CACHE_KEY_SALARIES + salaryDate);
            // 取得缓存数据
            byte[] salaries = (byte[])ctx.Cache[CACHE_KEY_SALARIES + salaryDate];
            // 缓存中无此数据，则读取文件数据
            if (salaries == null)
            {
                // 读取文件数据
                string appPath = Server.MapPath("");
                string crpptXmlFilePath = appPath + string.Format(SALARY_XML_FILE_PATH, salaryDate);
                // 读取文件字节流
                ;
                if (File.Exists(crpptXmlFilePath))
                {
                    salaries = File.ReadAllBytes(crpptXmlFilePath);

                    // 建立缓存数据
                    CacheDependency cacheDependency =
                       new CacheDependency(crpptXmlFilePath);
                    ctx.Cache.Insert(CACHE_KEY_SALARIES + salaryDate,
                             salaries,
                             cacheDependency,
                             Cache.NoAbsoluteExpiration,
                             Cache.NoSlidingExpiration,
                             CacheItemPriority.Default,
                             null);
                }
                else {
                    salaries = null;
                }

            }

            return salaries;
        }
        [WebMethod]
        public bool sendMail(string salaryDate) {
            //取得字节数据
            byte[] byteArr = GetSalaries(salaryDate);
            Secret sec = new Secret();
            //解密
            string str = sec.decrptString(byteArr);
            //转化为XML数据
            //List<XmlToPdf.Salary> lst = Secret.getXmlSalariesData(str);

            Progress progress = new Progress();
            //启动处理程序
            bool blnReturn = progress.Receive(str, salaryDate);
            //

            return blnReturn;
        }
        [WebMethod]
        public bool saveData(byte[] xmlData, string salaryDate)
        {
            if (xmlData == null || salaryDate.Length != 6)
            {
                return false;
            }
            else
            {
                // 文件存在确认
                string appPath = Server.MapPath("");
                string crpptXmlFilePath = appPath + string.Format(SALARY_XML_FILE_PATH, salaryDate);
                try
                {
                    File.WriteAllBytes(crpptXmlFilePath, xmlData);
                }
                catch (Exception e)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
